/**
 * 屠龙宝刀组件
 */
class DragonEffectUI extends BaseComp{
    private effect_dragon:eui.Group;
    private img_dragon:eui.Image;
    private img_light_1:eui.Image;
    private img_light_2:eui.Image;

    private timer:egret.Timer;
    private curIndex:number = 0;

    constructor()
    {
        super('DragonEffectView');
    }

    public init()
    {
        this.timer = new egret.Timer(60);
    }

    public addEvent()
    {
        this.timer.addEventListener(egret.TimerEvent.TIMER, this.timeHandle, this);
    }

    public timeHandle()
    {
        if(this.curIndex < 9)
        {
            let img = this['img_light_'+this.curIndex] as eui.Image;
            this.curIndex ++;
            egret.Tween.get(img).to({
                scaleY:1
            }, 200);
            WeChatgGameManager.instance().vibrateShort();
        }
        else
        {
            this.timer.stop();
        }
    }

    public cutAllFruit()
    {
        let logic:GameUI = UIManager.instance().getLogicByName(UIConst.GAME_UI) as GameUI;
        if(logic)
        {
            logic.cutAllFruit();
        }
    }

    public show()
    {
        let frame = 100/3;
        egret.Tween.get(this.img_dragon).to({
            x:this.width >> 1
        }, 5 * frame).to({
            x:(this.width >> 1) + 30
        }, 35 * frame).to({
            x:this.img_dragon.width/2 + this.width
        }, 5 * frame).set({
            x:-221
        })
    }

    public showLight()
    {
        let frame = 100/3;

        egret.Tween.get(this.img_light_1).to({
           scaleY :3
        },frame * 5).set({
            scaleY:0
        });
        egret.Tween.get(this.img_light_2).wait(4 * frame).to({
            scaleY:3
        },frame * 2).set({
            scaleY:0
        }).call(this.cutAllFruit, this);
    }

    public removeEvent()
    {
        this.timer.removeEventListener(egret.TimerEvent.TIMER, this.timeHandle, this);
    }

    public clear()
    {
        super.clear();
        this.timer.stop();
        this.timer = null;
        this.curIndex = null;
        EffectUtil.instance().StopEffect(this.img_dragon);
        EffectUtil.instance().StopEffect(this.img_light_1);
        EffectUtil.instance().StopEffect(this.img_light_2);
    }
}

window['DragonEffectUI'] = DragonEffectUI;